//-----------------Sonic CD Antlion Script-------------------//
//------------------Scripted by makasinaaaa-------------------//
// Yeah...

// Aliases
#alias Object.Value0		:	Object.Timer
#alias Object.Value1		:	Object.Timer2
#alias Object.Value2		:	Object.Timer3
#alias Object.Value3		:	Object.Timer4
//Object.Frame
#alias Object.Value4		:	Object.YVelocity
#alias Object.Value6		:	Object.YOriginPos

#alias Object.PropertyValue	:	Object.Quality


// States
#alias 0	:	ANTLION_IDLE
#alias 1	:	ANTLION_UP
#alias 2	:	ANTLION_ATTACK1
#alias 3	:	ANTLION_ATTACK2
#alias 4	:	ANTLION_DOWN

//140

// Badnik Quality / Property Values
#alias 0	:	GOOD_QUALITY
#alias 1	:	BAD_QUALITY

// Priority
#alias 0	:	PRIORITY_BOUNDS
#alias 1	:	PRIORITY_ACTIVE

sub ObjectMain
	CallFunction(StageSetup_CheckGoodFuture)	// Check if this should be a flower
		switch Object.State
			case ANTLION_IDLE
				if Object.PropertyValue == GOOD_QUALITY
					Object.YVelocity = -350000
				else
					Object.YVelocity = -50000
				end if
				break
			case ANTLION_UP
				if Object.PropertyValue == GOOD_QUALITY
					Object.YVelocity -= 40000
					Object.YPos += Object.YVelocity
					if Object.YVelocity < -880001
						Object.State = ANTLION_ATTACK1
						Object.YPos -= 50000
					end if
				else
					Object.YVelocity -= 4000
					Object.YPos += Object.YVelocity
					if Object.YVelocity < -270001
						Object.State = ANTLION_ATTACK1
						Object.YPos -= 50000
					end if
				end if
				break
			case ANTLION_ATTACK1
				Object.Timer++
				if Object.PropertyValue == GOOD_QUALITY
					if Object.Timer >= 4
						Object.Timer = 0
						if Object.Frame < 2
							Object.Frame++
						else
							Object.Timer = 0
							Object.State = ANTLION_ATTACK2
						end if
					end if
				else
					if Object.Timer >= 8
						Object.Timer = 0
						if Object.Frame < 2
							Object.Frame++
						else
							Object.Timer = 0
							Object.State = ANTLION_ATTACK2
						end if
					end if
				end if
				break
			case ANTLION_ATTACK2
				if Object.PropertyValue == GOOD_QUALITY
					Object.YVelocity = 350000
				else
					Object.YVelocity = 50000
				end if
				break
			case ANTLION_DOWN
				if Object.PropertyValue == GOOD_QUALITY
					if Object.YVelocity > 880001
						Object.YPos += 50000
						Object.State = ANTLION_IDLE
					else
						Object.YVelocity += 40000
						Object.YPos += Object.YVelocity
					end if
				else
					if Object.YVelocity > 270001
						Object.State = ANTLION_IDLE
						Object.YPos += 50000
					else
						Object.YVelocity += 4000
						Object.YPos += Object.YVelocity
					end if
				end if
				break
		end switch
	
	// Reset object when out of bounds
	if Object.OutOfBounds == true
		Object.Priority = PRIORITY_BOUNDS
		Object.State = ANTLION_IDLE
		Object.Timer = 0
		Object.YPos = Object.YOriginPos
	end if	
	CallFunction(StageSetup_CheckGoodFuture)	// Check if it should be a flower
end sub


sub ObjectPlayerInteraction
	switch Object.State
		case ANTLION_IDLE
			PlayerObjectCollision(C_TOUCH, -64, -200, 64, 0) //Detect
			if CheckResult == true
				Object.State = ANTLION_UP
			end if
			break
		case ANTLION_ATTACK2
			PlayerObjectCollision(C_TOUCH, -16, -64, 16, -13)
			if CheckResult == true
				if Object.Frame > 0
					Object.Frame--
				PlayStageSFX(4, false)
				else
					Object.Timer3 = 0
				end if
			else
				Object.Timer3++
				if Object.PropertyValue == GOOD_QUALITY
					if Object.Timer3 == 48
						Object.Timer3 = 0
						if Object.Frame < 2
							Object.Frame++
						end if
					end if
				else
					if Object.Timer3 == 96
						Object.Timer3 = 0
						if Object.Frame < 2
							Object.Frame++
						end if
					end if
				end if
			end if
			PlayerObjectCollision(C_TOUCH, -64, -64, 64, 0) //Detect
			if CheckResult == false
				Object.Timer++
				if Object.Timer >= 120
					Object.Timer2++
					if Object.PropertyValue == GOOD_QUALITY
						if Object.Timer2 == 4
							Object.Timer2 = 0
							if Object.Frame > 0
								Object.Frame--
							else
								Object.State = ANTLION_DOWN
								Object.Frame = 0
							end if
						end if
					else
						if Object.Timer2 == 8
							Object.Timer2 = 0
							if Object.Frame > 0
								Object.Frame--
							else
								Object.State = ANTLION_DOWN
								Object.Frame = 0
							end if
						end if
					end if
				end if
			else
				Object.Timer = 0
			end if
			break
	end switch
	switch Object.Frame
		case 0
			PlayerObjectCollision(C_TOUCH, -16, -8, 16, 23)
			if CheckResult == true
				CallFunction(Player_BadnikBreak)
			end if
			if Object.State == ANTLION_ATTACK2
				PlayerObjectCollision(C_TOUCH, -8, -24, 8, -23)
				if CheckResult == true
					CallFunction(Player_Hit)
				end if
			end if
			break
		case 1
			PlayerObjectCollision(C_TOUCH, -16, -8, 16, 23)
			if CheckResult == true
				CallFunction(Player_BadnikBreak)
			end if
			PlayerObjectCollision(C_TOUCH, -12, -24, 12, -8)
			if CheckResult == true
				CallFunction(Player_Hit)
			end if
			break
		case 2
			PlayerObjectCollision(C_TOUCH, -16, -1, 16, 23)
			if CheckResult == true
				CallFunction(Player_BadnikBreak)
			end if
			PlayerObjectCollision(C_TOUCH, -24, -13, 24, -1)// Spike Touch
			if CheckResult == true
				CallFunction(Player_Hit)
			end if
			break
	end switch
end sub


sub ObjectDraw
	DrawSprite(Object.Frame)
end sub


sub ObjectStartup
	LoadSpriteSheet("R2/Objects.gif")

	SpriteFrame(-16, -23, 32, 46, 149, 83)			// #0 - Antlion Frame 1
	SpriteFrame(-16, -23, 32, 46, 116, 83)			// #1 - Antlion Frame 2
	SpriteFrame(-24, -23, 48, 46, 67, 83)			// #2 - Antlion Frame 3
	
	// Used to be below LoadSpriteSheet, moved here for consistency
	ArrayPos0 = 32
	while ArrayPos0 < 1056
		if Object[ArrayPos0].Type == TypeName[Antlion]
			Object[ArrayPos0].YOriginPos = Object[ArrayPos0].YPos
		end if
		ArrayPos0++
	loop
end sub